package com.bootdo.quota.service.impl;

import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import com.bootdo.common.utils.DateUtils;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.quota.dao.QuotaHistoryDao;
import com.bootdo.quota.domain.QuotaHistoryDO;
import com.bootdo.quota.service.QuotaService;

/**
 * @Desc
 * @date 2020/1/14
 *
 */
@Service
public class QuotaServiceImpl implements QuotaService {

    @Autowired
    private QuotaHistoryDao quotaHistoryMapper;

    @Override
    public List<QuotaHistoryDO> list(Map<String, Object> map){
        //格式话日期
        String startDate = (String) map.get("startTime");
        String endDate = (String) map.get("endTime");
        if (StringUtils.isNotBlank(startDate)){
            startDate = DateUtils.getDateOfFormat(startDate, DateUtils.DATE_TIME_PATTERN);
            endDate = DateUtils.getDateOfFormat(endDate, DateUtils.DATE_TIME_PATTERN);
            map.put("startTime", startDate);
            map.put("endTime", endDate);
        }
        return quotaHistoryMapper.list(map);
    }

    @Override
    public int count(Map<String, Object> map){
        return quotaHistoryMapper.count(map);
    }

    @Override
    public HSSFWorkbook excelDownload(@RequestParam Map<String, Object> params){
        String startDate = (String) params.get("startTime");
        String endDate = (String) params.get("endTime");
        if (StringUtils.isNotBlank(startDate)){
            startDate = DateUtils.getDateOfFormat(startDate, DateUtils.DATE_TIME_PATTERN);
            endDate = DateUtils.getDateOfFormat(endDate, DateUtils.DATE_TIME_PATTERN);
            params.put("startTime", startDate);
            params.put("endTime", endDate);
        }

        List<QuotaHistoryDO> list = quotaHistoryMapper.list(params);

        //表头数据
        String[] header = {"序号", "平台订单号", "变动前金额", "变动类型", "变动金额", "变动后金额", "变更原因", "变更时间"};
        //声明一个工作簿
        HSSFWorkbook workbook = new HSSFWorkbook();
        //生成一个表格，设置表格名称为"学生表"
        HSSFSheet sheet = workbook.createSheet("额度流水");
        //设置表格列宽度为10个字节
        sheet.setDefaultColumnWidth(10);

        //创建第一行表头
        HSSFRow headrow = sheet.createRow(0);
        //遍历添加表头
        for (int i = 0; i < header.length; i++) {
            //创建一个单元格
            HSSFCell cell = headrow.createCell(i);
            //创建一个内容对象
            HSSFRichTextString text = new HSSFRichTextString(header[i]);
            //将内容对象的文字内容写入到单元格中
            cell.setCellValue(text);
        }

        for (int i = 0; i < list.size(); i++){
            HSSFRow row = sheet.createRow(i + 1);
            QuotaHistoryDO quota = list.get(i);

            //第一列
            HSSFCell cell = row.createCell(0);
            HSSFRichTextString text = new HSSFRichTextString(String.valueOf(i + 1));
            cell.setCellValue(text);

            //第二列  平台订单号
            cell = row.createCell(1);
            text = new HSSFRichTextString(quota.getOrderNo());
            cell.setCellValue(text);

            //第三列  变动前金额
            cell = row.createCell(2);
            text = new HSSFRichTextString(String.valueOf(quota.getQuota()));
            cell.setCellValue(text);

            //第四列  变动类型
            cell = row.createCell(3);
            if (quota.getChangeType() == 1){
                text = new HSSFRichTextString("增加");
            } else if (quota.getChangeType() == 2) {
                text = new HSSFRichTextString("减少");
            } else {
                text = new HSSFRichTextString("初始化");
            }

            cell.setCellValue(text);

            //第五列  变动金额
            cell = row.createCell(4);
            long money = Math.round(quota.getActualQuota() * 100) - Math.round(quota.getQuota() * 100);
            DecimalFormat df = new DecimalFormat("0.00");
            text = new HSSFRichTextString(String.valueOf(df.format((float)money/100)));
            cell.setCellValue(text);

            //第六列  变动后金额
            cell = row.createCell(5);
            text = new HSSFRichTextString(String.valueOf(quota.getActualQuota()));
            cell.setCellValue(text);

            //第七列  变动原因
            cell = row.createCell(6);
            text = new HSSFRichTextString(quota.getChangeReason());
            cell.setCellValue(text);

            //第八列  变动时间
            cell = row.createCell(7);
            text = new HSSFRichTextString(DateUtils.formatDate(quota.getGmtDate(), DateUtils.DATE_TIME_PATTERN));
            cell.setCellValue(text);

        }
        return workbook;
    }
}
